%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "contexts.h"
#include "debug.h"
  
  extern char* ctxctxtext;
  extern Context *ctx_tofill;
  
  extern int ctxctxlex();
  /* extern int ctxctxwrap(void); */  

  static int i1=0, i2=0;
  static void ctxctxerror(const char *str);

%}


%start ROOT

%token NAME
%token OPEN
%token CLOSED

%%

ROOT:
context
{
  return 1;
}


context: 
OPEN context_name 
OPEN ji_names CLOSED 
OPEN mi_names CLOSED
OPEN leqs CLOSED 
CLOSED

context_name:
empty 
| NAME {
  strncpy(ctx_tofill->name,ctxctxtext,CTX_NAME_DIM);
}


ji_names:
empty 
| NAME  {
  if(ctx_tofill->ji_no < CTX_MAX_JI)
    {
      char *ptr = malloc(strlen(ctxctxtext) +1);
      assert(ptr != NULL);
      strcpy(ptr,ctxctxtext);
      ctx_tofill->ji_names[ctx_tofill->ji_no] = ptr;
      ctx_tofill->ji_no++;
    }
  else
    {
      ctxctxerror("Too many join irreducibles");
    }
}
ji_names


mi_names:
empty 
| NAME  {
  if(ctx_tofill->mi_no < CTX_MAX_MI)
    {
      char *ptr = malloc(strlen(ctxctxtext) +1);
      assert(ptr != NULL);
      strcpy(ptr,ctxctxtext);
      ctx_tofill->mi_names[ctx_tofill->mi_no] = ptr;
      (ctx_tofill->mi_no)++;
    }
  else
    {
      ctxctxerror("Too many meet irreducibles");
    } 
}
mi_names


leqs:
empty 
| OPEN 
NAME 
{i1 = ctx_ji_index(ctx_tofill,ctxctxtext);}
NAME 
{i2 = ctx_mi_index(ctx_tofill,ctxctxtext); 
 if( (0 <= i1) && (0 <= i2) )
   ctx_tofill->leq[i1][i2] = 1;
}
CLOSED leqs


empty:

%%

void ctxctxerror(const char *str)
{
  fprintf(stderr,"error: %s\n",str); 
  exit(EXIT_FAILURE);
}


int ctxctxwrap(void) 
{ 
   return 1; 
}

